﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BbsbWebService.Enums;
using System.Runtime.Serialization;
using System.Xml.Linq;

namespace BbsbWebService.Tasks
{
    [DataContract]
    public class RecurringTask : BaseTask
    {
        public RecurringTask(DateTime nextAlarm, RecurringFrequency frequency, int magnitude)
            : base(nextAlarm)
        {
            Frequency = frequency;
            Magnitude = magnitude;
            SetNextAlarmMain();
        }

        public RecurringTask()
        {
            Magnitude = 1;
        }

        public RecurringTask(XElement element)
            : base(element)
        {
            Magnitude = element.GetChildValueFromElement<int>("Magnitude");
            Frequency = (RecurringFrequency)element.GetChildValueFromElement<int>("Frequency");
            SetNextAlarmMain();
        }

        [DataMember]
        public int Magnitude { get; set; }

        [DataMember(IsRequired=true)]
        public RecurringFrequency Frequency { get; set; }

        protected override void SetNextAlarm()
        {
            if (NextAlarm.HasValue && Magnitude > 0)
            {
                DateTime lastVal = NextAlarm.Value;
                switch (Frequency)
                {
                    case RecurringFrequency.Daily:
                        {
                            NextAlarm = lastVal.AddDays(Magnitude);
                            break;
                        }
                    case RecurringFrequency.Hourly:
                        {
                            NextAlarm = lastVal.AddHours(Magnitude);
                            break;
                        }
                    case RecurringFrequency.Monthly:
                        {
                            NextAlarm = lastVal.AddMonths(Magnitude);
                            break;
                        }
                    case RecurringFrequency.Weekly:
                        {
                            NextAlarm = lastVal.AddDays(Magnitude * 7);
                            break;

                        }
                }
            }
            else
            {
                NextAlarm = null;
            }
        }

        protected override string AdditionalDesctiption
        {
            get 
            {
                string frequncyConvert = string.Empty;
                switch (Frequency)
                {
                    case RecurringFrequency.Daily:
                        frequncyConvert = "day";
                        break;
                    case RecurringFrequency.Hourly:
                        frequncyConvert = "hour";
                        break;
                    case RecurringFrequency.Monthly:
                        frequncyConvert = "month";
                        break;
                    case RecurringFrequency.Weekly:
                        frequncyConvert = "week";
                        break;
                }

                string magConvert = string.Empty;
                string magExt = string.Empty;
                switch (Magnitude)
                {
                    case 1:
                        magConvert = string.Empty;
                        break;
                    case 2:
                        magConvert = "other ";
                        break;
                    default:
                        magConvert = string.Format("{0} ",Magnitude.ToString());
                        magExt = "s";
                        break;

                }

                return string.Format(" every {0}{1}{2}.", magConvert, frequncyConvert, magExt);
            }
        }

        protected override IEnumerable<XElement> GetRestAsXElement()
        {
            yield return new XElement("Magnitude", Magnitude);
            yield return new XElement("Frequency", (int)Frequency);
        }

        protected override void Validate(ref Classes.ValidationState state)
        {
            base.Validate(ref state);

            if (Magnitude < 1)
                state.AddError("Magnitude must be greater than zero");
        }
    }
}
